﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DanhMuc_DSDoanVienDanhDu : System.Web.UI.Page
{
    public static string donvi = "";
    readonly SecurityLib.Sec_AES _se = new SecurityLib.Sec_AES();
    public string st = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
            st = _se.Decrypt(Request.QueryString["tid"].Replace(" ", "+"), "gold", 128);
        }
    }
    protected void Btn_khoiphucdoanvien_Click(object sender, EventArgs e)
    {
        CheckBox select;
        string id = "";
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            select = (CheckBox)dr.FindControl("chkchon");
            if (select.Checked == true)
            {
                id = Grid_DSDV_Xoa.DataKeys[dr.RowIndex].Value.ToString().Trim();
                CatalogAccess.CapNhatTrangThaiDoanVien(id, 1);
                CatalogAccess.DeleteDoanVienDanhDu(id);
            }

        }
        BindGrid();
    }
    protected void Btn_xoadoanvien_Click(object sender, EventArgs e)
    {
        CheckBox select;
        string id = "";
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            select = (CheckBox)dr.FindControl("chkchon");
            if (select.Checked == true)
            {
                id = Grid_DSDV_Xoa.DataKeys[dr.RowIndex].Value.ToString().Trim();
                CatalogAccess.Xoa_PhucHoi_DoanVien(id, true);
            }
        }
        BindGrid();
    }
    protected void Btn_chuyensinhhoatdoan_Click(object sender, EventArgs e)
    {
        CheckBox select;
        string id = "";
        string tendoanvien = "";
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            select = (CheckBox)dr.FindControl("chkchon");
            if (select.Checked == true)
            {
                id = Grid_DSDV_Xoa.DataKeys[dr.RowIndex].Value.ToString().Trim();
                tendoanvien = ((Label)dr.FindControl("lblTenDoanVien")).Text;

            }
        }
        if (id != "")
        {
            SecurityLib.Sec_AES _ps = new SecurityLib.Sec_AES();
            Response.Redirect(PageURL.local + "DanhMuc/ChuyenSinhHoatDoan.aspx?id=" + _ps.Encrypt("4", "gold", 128) + "&subid=" + _ps.Encrypt("2", "gold", 128) + "&codeunionists=" + _ps.Encrypt(id.ToString(), "gold", 128) + "&nameunionists=" + _ps.Encrypt(tendoanvien, "gold", 128));
        }
        else
        {
            lblstatus.Visible = true;
            lblstatus.ForeColor = System.Drawing.Color.Red;
            lblstatus.Text = "Hãy chọn một đoàn viên trước khi chuyển!";
        }
    }
    protected void Btn_xuatfile_Click(object sender, EventArgs e)
    {
        Session.Remove("ListMaDoanVien_Print");
        Session.Remove("TenDonVi_Print");
        string listmadoanvien = "";
        CheckBox ckh;
        Label lbl;
        int count = 0;
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            ckh = (CheckBox)dr.FindControl("chkchon");
            if (ckh.Checked == true)
            {
                lbl = (Label)dr.FindControl("lblMaDoanVien");
                listmadoanvien += lbl.Text.Trim() + "!";
                count++;
            }
        }
        if (count > 0)
        {
            Session["ListMaDoanVien_Print"] = listmadoanvien;
            Session["TenDonVi_Print"] = _se.Decrypt(Request.QueryString["tid"].Replace(" ", "+"), "gold", 128);
            Response.Redirect("javascript:window.open('../Print/PrintPage.aspx','_blank');");
        }
        else
        {
            SecurityLib.ToolsMore.ShowPopUpMsg("Chưa chọn đoàn viên để in.", this.Page);
        }
    }
    protected void btn_timkiem_Click(object sender, EventArgs e)
    {
        BindGrid();
    }
    //chọn all checkbox
    protected void chkInGridviewHeader_CheckedChanged(object sender, EventArgs e)//day la ham de goi trong header cua itemtemplate
    {
        CheckBox chk = (CheckBox)sender;
        CheckBox chkitem;
        foreach (GridViewRow dr in Grid_DSDV_Xoa.Rows)
        {
            chkitem = (CheckBox)dr.FindControl("chkchon");
            if (chk.Checked == true)
            {
                chkitem.Checked = true;
            }
            else
            {
                chkitem.Checked = false;
            }
        }
    }
    
    //load du lieu vao gridview
    private void BindGrid()
    {
        if (txtHoten.Text.Length == 0)
        {
            _dbDataContext _db = new _dbDataContext();
            donvi = _se.Decrypt(Request.QueryString["dvid"].Replace(" ", "+"), "gold", 128);//MemberShip_ASP.GetMaDonViFromUserName(Page.User.Identity.Name);
            var val = from p in _db.tblDoanViens.Where(r => r.TrangThai == 7 && r.TrangThaiXoa == false && r.TrucThuoc.Trim() == donvi)
                      select new MemberShip_ASP.QueryAll
                      {
                          MaDoanVien = p.MaDonVien.Trim(),
                          TenDoanVien = p.HoTen.Trim(),
                          Phai = (p.Phai == true ? "Nam" : "Nữ"),
                          NgaySinh = string.Format("{0:dd/MM/yyyy}", p.NgaySinh),
                          CMND = p.CMND.Trim(),
                      };
            Grid_DSDV_Xoa.DataSource = val.ToList();
            Grid_DSDV_Xoa.DataBind();
        }
        else
        {
            _dbDataContext _db = new _dbDataContext();
            donvi = _se.Decrypt(Request.QueryString["dvid"].Replace(" ", "+"), "gold", 128);//MemberShip_ASP.GetMaDonViFromUserName(Page.User.Identity.Name);
            var val = from p in _db.tblDoanViens.Where(r => r.TrangThai == 7 && r.TrangThaiXoa == false && r.TrucThuoc.Trim() == donvi && r.HoTen.Contains(txtHoten.Text))
                      select new MemberShip_ASP.QueryAll
                      {
                          MaDoanVien = p.MaDonVien.Trim(),
                          TenDoanVien = p.HoTen.Trim(),
                          Phai = (p.Phai == true ? "Nam" : "Nữ"),
                          NgaySinh = string.Format("{0:dd/MM/yyyy}", p.NgaySinh),
                          CMND = p.CMND.Trim(),
                      };
            Grid_DSDV_Xoa.DataSource = val.ToList();
            Grid_DSDV_Xoa.DataBind();
        }
    }
    //autocomplete by name
    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetCompletionList_byname(string prefixText)
    {
        _dbDataContext _db = new _dbDataContext();
        var val = (from p in _db.tblDoanViens.Where(r => r.TrangThai == 7 && r.TrangThaiXoa == false && r.TrucThuoc.Trim() == donvi && r.HoTen.Trim().Contains(prefixText)) select p).ToList();
        string[] items = new string[val.ToList().Count];
        int i = 0;
        foreach (var item in val)
        {
            items.SetValue(item.HoTen.Trim(), i);
            i++;
        }
        return items;
    }
    protected void Grid_DSDV_Xoa_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = Grid_DSDV_Xoa.DataKeys[e.RowIndex].Value.ToString();
        if (CatalogAccess.Xoa_PhucHoi_DoanVien(id, true))
        {
            CatalogAccess.DeleteDoanVienDanhDu(id);
            BindGrid();
        }
        else
        {
            Response.Write("<script>alert('Error!');</script>");
        }
    }
    protected void Grid_DSDV_Xoa_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = Grid_DSDV_Xoa.DataKeys[e.RowIndex].Value.ToString();
        CatalogAccess.CapNhatTrangThaiDoanVien(id, 1);
        BindGrid();   
    }
}